﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Diagnostics;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class ListeNesneler : DevExpress.XtraEditors.XtraForm
    {
        public int Urun_Id=0;
        public int UrunTipi=0;

        public ListeNesneler()
        {
            InitializeComponent();
        }

        private void _btnYeni_Click(object sender, EventArgs e)
        {
            FormNesne NesneForm = new FormNesne();
            NesneForm.Urun_Id = Urun_Id;
            NesneForm.UrunTipi = UrunTipi;
            NesneForm.KayId = 0;
            NesneForm.ShowDialog();
            NesneListele();
        }


        private void ListeNesne_Load(object sender, EventArgs e)
        {
            Army.EventConfirm = false;

            //_txtAraString.Text = Urun_Id + " " + UrunTipi;
            NesneListele();
        }

        public void NesneListele()
        {
            string Sorgu = "Select \n" +
                "id [Nesne ID],\n" +
                "DosyaAdi [Dosya İsmi],\n" +
                "Aciklama [Açıklama]\n" +
                //"Nesne [Nesne] \n" +
                "	from Nesneler \n" +
            "where isDeleted=0 AND Urun_Id=" + Urun_Id + " AND UrunTipi=" + UrunTipi + " ORDER BY id Desc";


            DataTable dt = Army.SQL().GetDataTable(Sorgu);

            _listviewNesneler.Items.Clear();

            foreach (DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item[0].ToString());
                ls.SubItems.Add(item[1].ToString());
                ls.SubItems.Add(item[2].ToString());

                _listviewNesneler.Items.Add(ls);
            }

        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (_listviewNesneler.SelectedItems.Count > 0)
            {
                DataTable dt = Army.SQL().GetDataTable("Select DosyaAdi,Nesne From Nesneler where id=" + Convert.ToInt32(_listviewNesneler.SelectedItems[0].SubItems[0].Text));

                byte[] buffer = dt.AsEnumerable().Select(c => c.Field<byte[]>("Nesne")).SingleOrDefault();
                File.WriteAllBytes(@Application.StartupPath + "\\Temp\\" + dt.Rows[0][0].ToString(), buffer);

                //MessageBox.Show(@Application.StartupPath + "\\Temp\\" + dt.Rows[0][0].ToString() + " Kaydedildi!");

                try
                {
                    Process proc = new Process();
                    proc.StartInfo.FileName = @Application.StartupPath + "\\Temp\\" + dt.Rows[0][0].ToString();
                    proc.StartInfo.UseShellExecute = true;
                    proc.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }

            }
        }

        private void silToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Army.EventConfirm = false;
            if (_listviewNesneler.SelectedItems.Count > 0)
            {
                Army.EventMessage = "Seçilen nesne veritabanından silinecek\nEmin misiniz?";
                PopupAreYouSure Sor = new PopupAreYouSure();
                Sor.ShowDialog();

                if (Army.EventConfirm)
                {
                    Army.SQL().cmd("Update Nesneler set isDeleted=1 where id=" + Convert.ToInt32(_listviewNesneler.SelectedItems[0].SubItems[0].Text.ToString()));
                    NesneListele();
                }
            }
        }

        private void düzenleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormNesne NesneForm = new FormNesne();
            NesneForm.Urun_Id = Urun_Id;
            NesneForm.UrunTipi = UrunTipi;
            NesneForm.KayId = Convert.ToInt32(_listviewNesneler.SelectedItems[0].SubItems[0].Text);
            NesneForm.ShowDialog();
            NesneListele();
        }

        private void _btnAra_Click(object sender, EventArgs e)
        {

        }

        private void _btnListeGuncelle_Click(object sender, EventArgs e)
        {
            NesneListele();
        }



    }
}